const express = require('express');
const graphql = require('graphql');
const { graphqlHTTP } = require('express-graphql');

const UserType = new graphql.GraphQLObjectType({
  name: 'User',
  fields: {
    name: { type: graphql.GraphQLString },
    age: { type: graphql.GraphQLInt },
  },
});

const queryType = new graphql.GraphQLObjectType({
  name: 'Query',
  fields: {
    getUser: {
      type: UserType,
      args: {
        name: { type: graphql.GraphQLString },
      },
      resolve: (source, { name }) => {
        console.log(source, name);
        return {
          name,
          age: 18,
        };
      },
    },
    getNumArr: {
      type: new graphql.GraphQLList(graphql.GraphQLInt),
      args: {
        count: {
          type: graphql.GraphQLInt,
          require: true,
        },
      },
      resolve: (source, { count }) => {
        console.log(source, count);
        return new Array(count).fill(1);
      },
    },
  },
});

const schema = new graphql.GraphQLSchema({ query: queryType });

const app = express();

app.use(
  '/api/graphql',
  graphqlHTTP({
    schema,
    graphiql: true,
  })
);

app.listen(3000, () => {
  console.log('server is running in http://localhost:3000');
});
